package com.exercises;


import java.util.Arrays;

class Exercise881 {

    public static void main(String[] args) {
        System.out.println(numRescueBoats(new int[]{3, 2, 2, 1}, 3));
    }

    public static int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int result = 0;
        int i = 0, j = people.length - 1;
        while (j >= i) {
            if (people[i] + people[j] <= limit) {
                i++;
            }
            j--;
            result++;
        }
        return result;
    }

}

